package com.jilefojing.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.jilefojing.beans.enums.StatusEnumForPublish;
import com.jilefojing.beans.pojos.EBookChapter;
import com.jilefojing.beans.query.EBookChapterQuery;
import com.jilefojing.beans.vo.Pagination;
import com.jilefojing.dao.IEBookChapterDao;
import com.jilefojing.dao.base.impl.BaseDao;
import com.jilefojing.dao.mapper.IEBookChapterMapper;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年09月30日 20时22分10秒
 */
@Repository
public class EBookChapterDao extends BaseDao<EBookChapter> implements
		IEBookChapterDao {
	@Autowired
	private IEBookChapterMapper eBookChapterMapper;

	@Override
	public Pagination<EBookChapter> getPagination(long eBookId,
			Pagination<EBookChapter> pagination) {
		Example example = new Example(EBookChapter.class);
		example.createCriteria().andEqualTo(EBookChapter.FIELD_BOOKID, eBookId);
		return super.getPagination(pagination, example);
	}

	@Override
	public List<EBookChapter> getListByIds(List<Long> ids) {
		Example example = new Example(EBookChapter.class);
		example.createCriteria().andIn(EBookChapter.FIELD_ID, ids);
		return super.getListByExample(example);
	}

	@Override
	public EBookChapter getPrevInEBook(long eBookId, long id) {
		return eBookChapterMapper.getPrevInEBook(eBookId, id);
	}

	@Override
	public EBookChapter getNextInEBook(long eBookId, long id) {
		return eBookChapterMapper.getNextInEBook(eBookId, id);

	}

	@Override
	public Pagination<EBookChapter> getPagination(
			EBookChapterQuery eBookChapterQuery,
			Pagination<EBookChapter> pagination) {
		Example example = new Example(EBookChapter.class);
		Criteria criteria = example.createCriteria();

		if (Detect.notEmpty(eBookChapterQuery.getIds())) {
			criteria.andIn(EBookChapter.FIELD_ID, eBookChapterQuery.getIds());
		}

		if (Detect.notEmpty(eBookChapterQuery.getBookIds())) {
			criteria.andIn(EBookChapter.FIELD_BOOKID,
					eBookChapterQuery.getBookIds());
		}

		if (Detect.notEmpty(eBookChapterQuery.getStatuses())) {
			List<Short> statusCodes = new ArrayList<Short>();
			for (StatusEnumForPublish _Status : eBookChapterQuery.getStatuses()) {
				statusCodes.add(_Status.getCode());
			}
			criteria.andIn(EBookChapter.FIELD_STATUS, statusCodes);
		}

		if (Detect.notEmpty(eBookChapterQuery.getTitle())) {
			criteria.andEqualTo(EBookChapter.FIELD_TITLE,
					eBookChapterQuery.getTitle());
		}

		if (Detect.notEmpty(eBookChapterQuery.getLike_title())) {
			criteria.andLike(EBookChapter.FIELD_TITLE,
					"%" + eBookChapterQuery.getLike_title() + "%");
		}

		if (eBookChapterQuery.getEqualCreateTime() != null) {
			criteria.andEqualTo(EBookChapter.FIELD_CREATETIME,
					eBookChapterQuery.getEqualCreateTime());
		}

		if (Detect.notEmpty(eBookChapterQuery.getContent())) {
			criteria.andEqualTo(EBookChapter.FIELD_CONTENT,
					eBookChapterQuery.getContent());
		}

		if (Detect.notEmpty(eBookChapterQuery.getLike_content())) {
			criteria.andLike(EBookChapter.FIELD_CONTENT, "%"
					+ eBookChapterQuery.getLike_content() + "%");
		}

		if (Detect.notEmpty(eBookChapterQuery.getExtra())) {
			criteria.andEqualTo(EBookChapter.FIELD_EXTRA,
					eBookChapterQuery.getExtra());
		}

		if (Detect.notEmpty(eBookChapterQuery.getLike_extra())) {
			criteria.andLike(EBookChapter.FIELD_EXTRA,
					"%" + eBookChapterQuery.getLike_extra() + "%");
		}

		if (eBookChapterQuery.getEqualAndBeforCreateTime() != null) {
			criteria.andGreaterThanOrEqualTo(EBookChapter.FIELD_CREATETIME,
					eBookChapterQuery.getEqualAndBeforCreateTime());
		}

		if (eBookChapterQuery.getEqualAndAfterCreateTime() != null) {
			criteria.andLessThanOrEqualTo(EBookChapter.FIELD_CREATETIME,
					eBookChapterQuery.getEqualAndAfterCreateTime());
		}

		if (eBookChapterQuery.getBeforCreateTime() != null) {
			criteria.andGreaterThan(EBookChapter.FIELD_CREATETIME,
					eBookChapterQuery.getBeforCreateTime());
		}

		if (eBookChapterQuery.getAfterCreateTime() != null) {
			criteria.andLessThan(EBookChapter.FIELD_CREATETIME,
					eBookChapterQuery.getAfterCreateTime());
		}

		if (eBookChapterQuery.getEqualUpdateTime() != null) {
			criteria.andEqualTo(EBookChapter.FIELD_UPDATETIME,
					eBookChapterQuery.getEqualUpdateTime());
		}

		if (eBookChapterQuery.getEqualAndBeforUpdateTime() != null) {
			criteria.andGreaterThanOrEqualTo(EBookChapter.FIELD_UPDATETIME,
					eBookChapterQuery.getEqualAndBeforUpdateTime());
		}

		if (eBookChapterQuery.getEqualAndAfterUpdateTime() != null) {
			criteria.andLessThanOrEqualTo(EBookChapter.FIELD_UPDATETIME,
					eBookChapterQuery.getEqualAndAfterUpdateTime());
		}

		if (eBookChapterQuery.getBeforUpdateTime() != null) {
			criteria.andGreaterThan(EBookChapter.FIELD_UPDATETIME,
					eBookChapterQuery.getBeforUpdateTime());
		}

		if (eBookChapterQuery.getAfterUpdateTime() != null) {
			criteria.andLessThan(EBookChapter.FIELD_UPDATETIME,
					eBookChapterQuery.getAfterUpdateTime());
		}

		if (Detect.notEmpty(eBookChapterQuery.getCreateUserIds())) {
			criteria.andIn(EBookChapter.FIELD_CREATEUSER,
					eBookChapterQuery.getCreateUserIds());
		}
		if (Detect.notEmpty(pagination.getOrders())) {
			example.setOrderByClause(pagination.getOrdersToStr());
		}

		return super.getPagination(pagination, example);
	}
}
